package org.rulix.ogame.player.core.httpworker;

import java.io.FileInputStream;
import java.util.List;
import java.util.concurrent.Callable;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.rulix.ogame.player.api.OgameFleetMovement;
import org.rulix.ogame.player.core.OgameCoreLogicImpl;
import org.rulix.ogame.player.core.OgameSession;
import org.rulix.ogame.player.core.http.OgameRequestFactory;
import org.rulix.ogame.player.core.info.OgameDataHelper;

public class HttpEventListWorker implements Callable<Integer> {

	private OgameCoreLogicImpl logic;
	
	public HttpEventListWorker(OgameCoreLogicImpl logic) {
		this.logic = logic;
	}
	
	@Override
	public Integer call() throws Exception {
		
		HttpClient client = logic.getHttpClient();
		OgameSession ogameSession = logic.getOgameSession();
		
		HttpGet request = OgameRequestFactory.getEventListRequest(ogameSession);
		HttpResponse response = client.execute(request);
		
		//TODO: check response is not an error response.
		OgameDataHelper odh = new OgameDataHelper();
		
//		StringBuilder output = OgameDataHelper.readInputStream(response.getEntity().getContent());
		//StringBuilder output = OgameDataHelper.readInputStream(response.getEntity().getContent());
		StringBuilder output = OgameDataHelper.readInputStream(new FileInputStream("/Users/rulix/Desktop/Ataque_Ogame.html"));
		List<OgameFleetMovement> list = odh.getFleetMovements(output);
		logic.setFleetMovements(list);
		logic.notifyListenersFleetMovementsAvailable();
		response.getEntity().getContent().close();
		return 0;
	}

}
